package com.sec.etech.employeesync.util;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sec.etech.employeesync.entity.OrgEmployee;
import com.sec.etech.employeesync.mapper.OrgEmployeeMapper;
import com.sec.etech.employeesync.mapper.OrgEmployeeZLevelJobRelationMapper;
import com.sec.etech.employeesync.mapper.OrgEmployeeZTertiaryLevelJobMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : 齐马的作品
 * @version : 1.0
 * @date : 2024/8/26 上午1:32
 * @description:
 */
@Component
@Slf4j
public class EmployeeFilterHandler {

    @Resource
    private OrgEmployeeZLevelJobRelationMapper levelJobRelationMapper;
    @Resource
    private OrgEmployeeZTertiaryLevelJobMapper tertiaryLevelJobMapper;
    @Resource
    private OrgEmployeeMapper orgEmployeeMapper;

    private final Map<String, Integer> confirmMap = new HashMap<>();
    private final List<String> originList = new ArrayList<>();

    public Boolean isEmployeeFilter(JSONObject employeeData) {
        // 三级岗位目录
//        String zsjgwml = employeeData.getString("ZSJGWML");
//        Integer comfirm = confirmMap.get(zsjgwml);
//
//        String account = employeeData.getString("PERNR");
//
//        return (comfirm != null && comfirm == 1) || originList.contains(account);
        return true;
    }

    public void initConfirmMap() {
        List<JSONObject> list = levelJobRelationMapper.queryAllConfirm();
        Map<String, Integer> map = list.parallelStream()
                .collect(Collectors.toMap(jsonObject -> jsonObject.getString("code"),
                        jsonObject -> jsonObject.getInteger("confirm")));
        confirmMap.clear();
        confirmMap.putAll(map);
    }

    public void initOriginList() {
        List<OrgEmployee> list = orgEmployeeMapper.selectList(Wrappers.<OrgEmployee>lambdaQuery()
                .eq(OrgEmployee::getOrigin, 2));

        List<String> accountList = list.parallelStream()
                .map(OrgEmployee::getAccount)
                .collect(Collectors.toList());

        originList.clear();
        originList.addAll(accountList);
    }
}
